﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using BrewXML.Core.Data;
using BrewXML.Core.Services;
using BrewXML.Core.Services.NavigationServices;
using BrewXML.Core.ViewModels;
using BrewXML.WindowsPhone.Data;
using Cirrious.CrossCore;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace BrewXML.WindowsPhone
{
    public partial class GrainsPage : PhoneApplicationPage,IView
    {
        #region View Model
        public HomePageViewModel ViewModel
        {
            get
            {
                return this.DataContext as HomePageViewModel;
            }
        }
        #endregion

        #region OnNavigateTo

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //init();
            base.OnNavigatedTo(e);
        }
        #endregion

        #region init
        public void init()
        {
            var list = XMLClass.Load<List<GRAIN>>("Grains.xml", "GRAINS");
            ViewModel.GRAINS = list.Result;
        }
        #endregion

        #region Build Application Bar
        private void BuildApplicationBar()
        {
            var appBar = new ApplicationBar() { Opacity = 1, Mode = ApplicationBarMode.Default };
            var btnAdd = new ApplicationBarIconButton(new Uri("/Asset/add.png", UriKind.Relative)) { Text = "Add" };
            btnAdd.Click += (sender, args) =>
            {
                var tmp = new GRAIN();
                Mvx.Resolve<GrainDetailPageViewModel>().Grain = tmp;
                Mvx.Resolve<GrainDetailPageViewModel>().AllowAdd = true;
                Mvx.Resolve<INavigationService>().NavigateTo(PageNames.GrainDetailPage);
            };
            appBar.Buttons.Add(btnAdd);
            var btnRefresh = new ApplicationBarIconButton(new Uri("/Asset/refresh.png", UriKind.Relative)) { Text = "Refresh" };
            btnRefresh.Click += (sender, args) =>
            {
                this.Refresh();
            };
            appBar.Buttons.Add(btnRefresh);
            this.ApplicationBar = appBar;
        }
        #endregion

        public GrainsPage()
        {
            InitializeComponent();
            BuildApplicationBar();
            init();
        }

        public void Refresh()
        {
            init();
        }

        private void TxtBox_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.txtBox.Text != "")
            {
                var tmplist = ViewModel.GRAINS.Where(s => s.NAME.Contains(this.txtBox.Text) || s.TYPE.Contains(this.txtBox.Text)).ToList();
                ViewModel.GRAINS = null;
                ViewModel.GRAINS = tmplist;
            }
            else
            {
                var list = XMLClass.Load<List<GRAIN>>("Grains.xml", "GRAINS");
                ViewModel.GRAINS = list.Result;
            }
        }
    }
}